<?php
namespace App\Services;

use App\Models\WxMaterialCategory;
use Illuminate\Support\Facades\Cache;

/**
 * 微信素材
 * @package App\Services
 */
class WxMaterialService
{
    //素材类型常量
    const TYPE_NEWS = 'news';
    const TYPE_IMAGE = 'image';
    const TYPE_VOICE = 'voice';
    const TYPE_VIDEO = 'video';
    const TYPE_THUMB = 'thumb';
    const TYPE_MUSIC = 'music';

    private $typeList;//类别列表

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->typeList = [
            self::TYPE_NEWS => __NAMESPACE__ . '\WxMaterial\WxMaterialNews',
            self::TYPE_IMAGE => __NAMESPACE__ . '\WxMaterial\WxMaterialImage',
            self::TYPE_VOICE =>__NAMESPACE__ . '\WxMaterial\WxMaterialVoice',
            self::TYPE_VIDEO => __NAMESPACE__ . '\WxMaterial\WxMaterialVideo',
            self::TYPE_THUMB => __NAMESPACE__ . '\WxMaterial\WxMaterialThumb',
            self::TYPE_MUSIC => __NAMESPACE__ . '\WxMaterial\WxMaterialMusic',
        ];
    }

    /**
     * 获取类别列表
     * @return array
     */
    public function getMaterialTypeList()
    {
        $typeList = [
            self::TYPE_NEWS => '图文素材',
            self::TYPE_IMAGE => '图片素材',
            self::TYPE_VOICE => '语音素材',
            self::TYPE_VIDEO => '视频素材',
        ];
        return $typeList;
    }
    /**
     * 获取分类列表
     * @param $type
     * @return array
     */
    public function getCatList($type)
    {
        $menuList = WxMaterialCategory::where('material_type', $type)->orderBy('order_num','ASC')->orderBy('cat_id','ASC')->get()->toArray();
        $newMenuList = [];
        foreach ($menuList as $v){
            $newMenuList[(int)$v['cat_id']] = $v;
        }
        return $newMenuList;
    }

    /**
     * 获取缓存中中的分类列表
     * @param $type
     * @param bool $isRefresh
     * @return mixed
     */
    public function getCacheCatList($type, $isRefresh = false)
    {
        $cacheKey = 'wxmaterial_catlistbygroupkey_'.$type;
        $cacheData = Cache::get($cacheKey);
        if (!$cacheData || $isRefresh){
            $catData = $this->getCatList($type);
            Cache::put($cacheKey, $catData, 60);
        }
        return $cacheData;
    }
    /**
     * 保存分类
     * @param $request
     * @return bool
     */
    public function saveCat($request)
    {
        if ($request->cat_id){
            $catModel = WxMaterialCategory::find($request->cat_id);
        }else{
            $catModel = new WxMaterialCategory;
        }
        $catModel->parent_id = (int) $request->parent_id;
        $catModel->material_type = $request->material_type;
        $catModel->name = $request->name;
        $catModel->description = $request->description;
        $catModel->name = $request->name;
        return $catModel->save();
    }

    /**
     * 删除分类
     * @param $catId
     * @return mixed
     */
    public function delCat($catId)
    {
        $catModel = WxMaterialCategory::find($catId);
        return $catModel->delete();
    }
    /**
     * 根据类别创建素材操作对象
     * @param $type
     * @return WxMaterial\wxMaterialInterface
     */
    public function createMaterial($type)
    {
        if (!array_key_exists($type, $this->typeList)){
            throw new \InvalidArgumentException("$type 无效的素材类型");
        }
        return new $this->typeList[$type];
    }

}